package com.etcn.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.etcn.common.Enum.DateFormat;

/**
 * 时间工具类
 * 
 * @author guozc
 */
public class DateUtil {

	/**
	 * 格式化日期为制定格式的字符串
	 * 
	 * @param argDate
	 * @param argFormat
	 * @return 格式化后的日期字符串
	 */
	public static String formatDateToString(Date argDate, DateFormat argFormat) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat(argFormat.value());
		return format.format(argDate).toString();
	}

	/**
	 * 格式化字符串为制定格式的日期
	 * 
	 * @param argDateStr
	 * @param argFormat
	 * @return
	 */
	public static Date formatStringToDate(String argDateStr, DateFormat argFormat) throws Exception {
		SimpleDateFormat sdfFormat = new SimpleDateFormat(argFormat.value());
		// 严格模式
		sdfFormat.setLenient(false);
		try {
			return sdfFormat.parse(argDateStr);
		} catch (ParseException e) {
			throw new Exception(e);
		}
	}

	/**
	 * 获取制定格式的当前时间字符串
	 * 
	 * @param argFormat
	 * @return
	 * @throws Exception
	 */
	public static String getNowStr(DateFormat argFormat) throws Exception {
		return formatDateToString(new Date(), argFormat);
	}

	/**
	 * 获取制定格式的当前时间
	 * 
	 * @param argFormat
	 * @return
	 * @throws Exception
	 */
	public static Date getNowDate(DateFormat argFormat) throws Exception {
		return formatStringToDate(getNowStr(argFormat), argFormat);
	}

	/**
	 * 智能转换日期
	 * 
	 * @param date
	 * @return
	 */
	public static String smartFormat(Date date) {
		String dateStr = null;
		if (date == null) {
			dateStr = "";
		} else {
			try {
				dateStr = formatDateToString(date, DateFormat.Y_M_D_HMS);
			} catch (Exception ex) {
				throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex);
			}
		}
		return dateStr;
	}

	/**
	 * 智能转换日期
	 * 
	 * @param text
	 * @return
	 */
	public static Date smartFormat(String text) {
		Date date = null;
		try {
			if (text == null || text.length() == 0) {
				date = null;
			} else if (text.length() == 10) {
				date = formatStringToDate(text, DateFormat.Y_M_D);
			} else if (text.length() == 13) {
				date = new Date(Long.parseLong(text));
			} else if (text.length() == 16) {
				date = formatStringToDate(text, DateFormat.Y_M_D_HM);
			} else if (text.length() == 19) {
				date = formatStringToDate(text, DateFormat.Y_M_D_HMS);
			} else {
				throw new IllegalArgumentException("日期长度不符合要求!");
			}
		} catch (Exception e) {
			throw new IllegalArgumentException("日期转换失败!");
		}
		return date;
	}

	/**
	 * 时间比较
	 * 
	 * @param t1
	 *            时间1
	 * @param t2
	 *            时间2
	 * @param argFormat
	 *            时间格式
	 * @return 0:相等,1:t1>t2,-1:t1<t2
	 * @throws Exception
	 */
	public static int timeCompare(String t1, String t2, DateFormat argFormat) throws Exception {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(formatStringToDate(t1, argFormat));
			c2.setTime(formatStringToDate(t2, argFormat));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		return result;
	}

	/**
	 * 时间比较
	 * 
	 * @param t1
	 *            时间1
	 * @param t2
	 *            时间2
	 * @param argFormat
	 *            时间格式
	 * @return 0:相等,1:t1>t2,-1:t1<t2
	 * @throws Exception
	 */
	public static int timeCompare(Date t1, Date t2, DateFormat argFormat) throws Exception {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(formatStringToDate(formatDateToString(t1, argFormat), argFormat));
			c2.setTime(formatStringToDate(formatDateToString(t2, argFormat), argFormat));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		return result;
	}

	public static String getCurrentYearStr() throws Exception {
		return getNowStr(DateFormat.YYYY);
	}

	public static String getYearStr(Date date, DateFormat argFormat) throws Exception {
		return formatDateToString(date, argFormat);
	}
	
	/**
	 * 日期date上加count天，count为负表示减
	 */
	public static Date addDay(Date date, int count) throws Exception{
		return new Date(date.getTime() + 86400000L * count);
	}
	
	/**
	 * 日期date上加count分钟，count为负表示减
	 */
	public static Date addMinute(Date date, int count) {
		return new Date(date.getTime() + 60000L * count);
	}

	/**
	 * 日期date上加count小时，count为负表示减
	 */
	public static Date addHour(Date date, int count) {
		return new Date(date.getTime() + 3600000L * count);
	}
	

	/**
	 * 日期date上加count星期，count为负表示减
	 */
	public static Date addWeek(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.WEEK_OF_YEAR, count);
		return c.getTime();
	}

	/**
	 * 日期date上加count月，count为负表示减
	 */
	public static Date addMonth(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, count);
		return c.getTime();
	}

	/**
	 * 日期date上加count年，count为负表示减
	 */
	public static Date addYear(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, count);
		return c.getTime();
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(addDay(new Date(), 365));
	}

}